package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @description:
 * @author: ajie
 * @date: 2024/4/18 16:05:44
 * @version: 1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor//使用构造器 Lombok是在编译期生成相应的方法
public class LessonChangeListener {

    final ILearningLessonService lessonService;

//    public LessonChangeListener(ILearningLessonService lessonService) {
//        this.lessonService = lessonService;
//    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.pay.queue",durable = "true"),
    exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
    key = MqConstants.Key.ORDER_PAY_KEY))
    public  void onMsg(OrderBasicDTO dto){
        log.info("LessonChangeListener 接受到了消息 用户{} 添加课程{}",dto.getUserId(),dto.getCourseIds());
        //校验
        if (dto == null ||dto.getUserId() == null ||dto.getCourseIds() == null || CollUtils.isEmpty(dto.getCourseIds())){
            //不要抛异常，否则开启了重试
            return;//消费了
        }

        //调用service 保存课程到课表
        lessonService.addUserLesson(dto.getUserId(),dto.getCourseIds());

    }
}
